<?php

namespace App\Http\Controllers\Strator;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Strator\Traits\StratorTokenTrait;
use App\Http\Requests\Strator\StratorLoginRequest;
use App\Models\Strator\Strator;
use App\Models\Strator\StratorAuthToken;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class StratorLoginController extends Controller
{
    use StratorTokenTrait;

    //
    public function login(StratorLoginRequest $request,Strator $strator)
    {
        $captchaCheckFail=$this->capchaCheck($request);
        if($captchaCheckFail){
            return $this->fail($captchaCheckFail);
        }

        if(!$request->filled('strator_account') && !$request->filled('strator_mobile') && !$request->filled('strator_email') ){
            return $this->fail('登录账号不能为空');
        }
        $strator=$this->getStrator($request, $strator);
        $stratorCheckFail=$this->stratorCheck($request,$strator);
        if($stratorCheckFail){
            return $this->fail($stratorCheckFail);
        }
        $this->createLoginLog($request,$strator,1,'');
        return $this->success([
            'token'=>$this->generateToken($request,$strator),
            'strator'=>$strator,
        ],
            '登录成功'
        );
    }
    //
    public function capchaCheck($request):string{
        $res=cache($request->captcha_key);
        if(empty($res)){
            return '验证码key错误或者已过期';
        }
        if($res!==$request->captcha_res){
            cache()->forget($request->captcha_key);
            return '验证码计算结果错误';
        }
        //
        cache()->forget($request->captcha_key);
        return '';
    }
    //
    public function stratorCheck($request,$strator):string{
        if(empty($strator)){
            $this->createLoginLog($request,$strator,0,'账号错误，无此账号');
            return '账号或密码错误';
        }
        if($strator->is_suspended==1){
            $this->createLoginLog($request,$strator,0,'账号已被禁用，无法登录');
            return '账号已被禁用，无法登录';
        }
        if(!Hash::check($request->strator_password,$strator->strator_password)){
            $this->createLoginLog($request,$strator,0,'密码错误');
            return '账号或密码错误';
        }
        return '';
    }
    //
    public function getStrator($request, Strator $strator): ?Strator{
        if($request->filled('strator_account')){
            return $strator->with(['positions'])->where('strator_account',$request->strator_account)->first();
        }
        if($request->filled('strator_mobile')){
            return $strator->with(['positions'])->where('strator_mobile',$request->strator_mobile)->first();
        }
        if($request->filled('strator_email')){
            return $strator->with(['positions'])->where('strator_email',$request->strator_email)->first();
        }
        return null;
    }

    //
    public function loginStratorInfo(Request $request){
        $loginStrator=$request->user();
        $loginStrator->loadMissing(['positions']);
        return $this->success($loginStrator);
    }

    //
    public function logout(Request $request,Strator $strator)
    {
        StratorAuthToken::where('token',$request->bearerToken())->delete();
        return $this->successMsg('退出成功');
    }


}
